package com.sky.robot.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sky.robot.entity.SkyMethodAspect;
import com.sky.robot.mapper.SkyMethodAspectMapper;
import com.sky.robot.service.SkyMethodAspectService;
import com.sky.robot.utils.JSONUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class SkyMethodAspectServiceImpl extends ServiceImpl<SkyMethodAspectMapper, SkyMethodAspect> implements SkyMethodAspectService {

    @Autowired
    private SkyMethodAspectMapper skyMethodAspectMapper;

    @Override
    public IPage<SkyMethodAspect> listPage(IPage page, Map<String, Object> map) {
        // 查询分页数据
        List<SkyMethodAspect> list = skyMethodAspectMapper.listPage(page, map);
        // 查询一共多少行数据
        Map<String, Object> mPage = skyMethodAspectMapper.sum(map);
        page.setRecords(list);
        page.setTotal((long) mPage.get("total" ));
        return page;
    }

    @Override
    @Transactional(readOnly = true)
    public Object get(Map<String, Object> params) {
        String id = (String) params.get("id" );
        SkyMethodAspect skyTableColumn = getById(id);
        if (skyTableColumn != null) {
            String json = JSONUtils.beanToJson(skyTableColumn);
            Map<String, Object> map = JSONUtils.jsonToMap(json);
            map.putAll(params);
            params = map;
        }
        return params;
    }


    @Override
    @Transactional
    public SkyMethodAspect add(SkyMethodAspect skyTableColumn) {
        save(skyTableColumn);
        return skyTableColumn;
    }

    @Override
    @Transactional
    public SkyMethodAspect edit(SkyMethodAspect skyTableColumn) {
        updateById(skyTableColumn);
        return skyTableColumn;
    }

    @Override
    @Transactional
    public Object delete(String id) {
        removeById(id);
        return null;
    }

    @Override
    @Transactional
    public Object deleteBatch(String ids) {
        List<String> idList = new ArrayList<>();
        for(String id : ids.split(",")){
            idList.add(id);
        }
        removeByIds(idList);
        return null;
    }

}
